package com.example.demo.orm.controller;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.solon.annotation.Db;
import com.example.demo.orm.model.DeptEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.noear.solon.annotation.*;

@Controller
@Mapping("/dept")
@Api("部门管理")
public class DeptController {
  @Db("db1")
  private EasyEntityQuery entityQuery;

  @Mapping
  @Get
  @ApiOperation("获取列表")
  public List<DeptEntity> list() {
    return entityQuery.queryable(DeptEntity.class).toList();
  }

  @Mapping
  @Post
  @ApiOperation("新增")
  public Boolean add(@Body DeptEntity dept) {
    return entityQuery.insertable(dept).executeRows(true) > 0L;
  }

  @Mapping
  @Put
  @ApiOperation("更新")
  public Boolean update(@Body DeptEntity dept) {
    return entityQuery.updatable(dept).executeRows() > 0L;
  }

  @Mapping("/{id}")
  @Get
  @ApiOperation("获取")
  public DeptEntity get(Long id) {
    return entityQuery.queryable(DeptEntity.class).whereById(id).firstOrNull();
  }

  @Mapping("/{id}")
  @Delete
  @ApiOperation("删除")
  public Boolean delete(Long id) {
    return entityQuery
            .getEasyQueryClient()
            .deletable(DeptEntity.class)
            .allowDeleteStatement(true)
            .whereById(id)
            .executeRows()
        > 0L;
  }
}
